package com.tianji.learning;

import java.util.concurrent.Semaphore;

public class ABCTest1 {
    public static void main(String[] args) throws Exception {

        Semaphore semaphore1 =new Semaphore(1);
        Semaphore semaphore2 =new Semaphore(0);
        Semaphore semaphore3 =new Semaphore(0);

        Thread t1 = new Thread(() -> {
            try {
                while (true) {
                    semaphore1.acquire();
                    System.out.println(Thread.currentThread().getName() + ":A");
                    semaphore2.release();
                }
            } catch (Exception e) {
            }
        }, "Thread-A");

        Thread t2 = new Thread(() -> {
            try {
                while (true) {
                    semaphore2.acquire();
                    System.out.println(Thread.currentThread().getName() + ":B");
                    semaphore3.release();
                }
            } catch (Exception e) {
            }
        }, "Thread-B");

        Thread t3 = new Thread(() -> {
            try {
                while (true) {
                    semaphore3.acquire();
                    System.out.println(Thread.currentThread().getName() + ":C");
                    semaphore1.release();
                }
            } catch (Exception e) {
            }
        }, "Thread-C");


        t1.start();
        t2.start();
        t3.start();

    }
}